package ribbon.consumer.xytest;

import com.xylink.config.SDKConfigMgr;
import com.xylink.config.SignatureVersion;
import com.xylink.log.XyLinkLogType;
import com.xylink.model.MeetingInfo;
import com.xylink.model.SdkMeeting;
import com.xylink.sdk.SdkMethodV1;
import com.xylink.sdk.SdkMethodV2;
import com.xylink.util.Result;
import com.xylink.util.SignUtil;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;

import java.io.IOException;
import java.util.Base64;
import java.util.HashMap;
import java.util.Map;

public class CreateMeetingRoom_sign2_0 {

//    public static void main(String[] args) throws IOException {
//        //初始化配置
//        //初始化单个企业的
//        String apiHost = "https://sdkapi.xylink.com";
//        String clientId = "FOgTJNxccxrYsWqwYWkQpob6";
//        String clientSecret = "a6xxlm5mPrwHsSr2rrPVyX1v0jNXYBu6";
//        String enterpriseId = "b42dbfcba33cdcf20ecf97f82d9605d284dfd81b";
//        //此处列出了所有的可用配置，用户需要根据实际情形进行填充
//        //SignatureVersion 是签名版本，如果无特殊需求请设置SignatureVersion.Signature_2_0
//        //SignUtil 为签名算法，如果无特殊需求请设置SignUtil.SignType.MD5
//        //enableSecurityHttp 为了安全考虑，我们会将DELTE、PUT替换为POST进行处理，如无特殊需求，请置为false
//        //extrasHeader 如果需要请求的时候带上额外的header
//        //我们支持多种日志输出方式，请根据实际情况进行选择配置
//        SDKConfigMgr.initSDKConfig(apiHost, clientId, clientSecret, enterpriseId, SignatureVersion.Signature_2_0, SignUtil.SignType.MD5, false,null, XyLinkLogType.Log4j);
//
//        //至此，jar包已经初始化完成，下面将是我们的调用环节, 签名2.0请使用SdkMethodV2，不要使用SdkMethodV1
//        //此处我们列举了创建会议室和查询会议室接口示例
//
//        //1.创建会议室
//        String url = "/api/rest/external/v1/create_meeting?enterpriseId=" + enterpriseId;
//        HashMap<String, Object> body = new HashMap<>();
//        body.put("meetingName", "sdk测试");
//        Result<SdkMeeting> result1 = SdkMethodV2.sdkPost(url, body, SdkMeeting.class);
//        if (result1.isSuccess()){
//            System.out.println("result: " + result1);
//        }else {
//            System.out.println(result1);
//            throw new RuntimeException("faild to Create Meeting Room");
//        }
//
////        //2.查询会议室
////        String meetingRoomNumber = result1.getData().getMeetingNumber();
////        String url1 = "/api/rest/external/v1/meetingInfo/"+meetingRoomNumber+"?enterpriseId=" + enterpriseId;
////        Result<MeetingInfo> result2 = SdkMethodV2.sdkGet(url1, MeetingInfo.class);
////        if (result2.isSuccess()){
////            System.out.println("result: " + result2);
////        }else {
////            System.out.println("result: " + result2);
////            throw new RuntimeException("faild to get Meeting Room");
////        }
////
////        //下面是调用post的示例，DELETE、PUT调用相应的方法即可
//////        Map<String,Object> body = new HashMap<String,Object>();
//////        Result<Map> mapResult = SdkMethodV1.sdkPost("", token, body, Map.class);
//
//      }

    public static void main(String[] args) throws IOException {

//
//        String apiHost = "https://sdkapi.xylink.com";
//        String clientId = "iWKOmgXBWeOCi1B2zTspDC8x";
//        String clientSecret = "K72TZg20gQeIll8xf8HbQyiWe1sTy9wc";
//        String enterpriseId = "59d17835b04e0b45266973dccb8b5d8cb8a7b490";

        // 创建HttpClient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();



        //测试环境信息配置
        String apiHost = "https://sdkapi.xylink.com";
        String clientId = "F0gTJNxccxrYSWqWYWkQpob6";
        String clientSecret = "a6xxlm5mPrwHsSr2rrPVyX1v0jNXYBu6";
        String enterpriseId = "b42dbfcba33cdcf20ecf97f82d9605d284dfd81b";

        SDKConfigMgr.initSDKConfig(apiHost, clientId, clientSecret, enterpriseId, SignatureVersion.Signature_3_0);

        String url = "/api/rest/external/v1/create_meeting?enterpriseId=" + enterpriseId;
        //String url = "/api/rest/external/v1/meetingreminders/9681baa08fb01a2e019077d679a971e3?enterpriseId=" + enterpriseId;
//        String url = "/api/rest/external/v1/conference/reminder/query?reminderId=96806fa08fb01e83019077e6a3086d7b&enterpriseId=" + enterpriseId;

        HashMap<String, Object> body = new HashMap<>();
        body.put("meetingName", "sdk测试");

        Result<SdkMeeting> result1 = SdkMethodV2.sdkPost(url, body, SdkMeeting.class);
        if (result1.isSuccess()) {
            System.out.println("result: " + result1);
            SdkMeeting data = result1.getData();
            // 获取Base64编码器
            Base64.Encoder encoder = Base64.getEncoder();
            // 将字符串转换为字节数组，然后使用Base64编码器进行编码
            String encodedString = encoder.encodeToString(data.getControlPassword().getBytes());
            System.out.println("控制密码===原始字符串：" + data.getControlPassword());
            System.out.println("控制密码===Base64编码后的字符串：" + encodedString);

            String meetingNumberString = encoder.encodeToString(data.getMeetingNumber().getBytes());

            System.out.println("控制密码===会议编号字符串：" + data.getMeetingNumber());
            System.out.println("控制密码===会议编号Base64编码后的字符串：" + meetingNumberString);
           // 创建HttpGet请求
            HttpGet httpGet = new HttpGet("xylink://joinMeeting?number="+data.getMeetingNumber()+"&topic=&pwd=&displayName=&autoAudioMute=true&autoVideoMute=true&contentOnlyMode=true&quitWhenCallFinish=true");

            // 执行请求
            CloseableHttpResponse response = httpClient.execute(httpGet);
        }else {
            System.out.println(result1);
            throw new RuntimeException("faild to Create Meeting Room");
        }


//        //2.查询会议室
//        String meetingRoomNumber = result1.getData().getMeetingNumber();
//        String url1 = "/api/rest/external/v1/meetingInfo/"+meetingRoomNumber+"?enterpriseId=" + enterpriseId;
//        Result<MeetingInfo> result2 = SdkMethodV2.sdkGet(url1, MeetingInfo.class);
//        if (result2.isSuccess()){
//            System.out.println("result: " + result2);
//        }else {
//            System.out.println("result: " + result2);
//            throw new RuntimeException("faild to get Meeting Room");
//        }
//
//        //下面是调用post的示例，DELETE、PUT调用相应的方法即可
//        Map<String,Object> body = new HashMap<String,Object>();
//        Result<Map> mapResult = SdkMethodV1.sdkPost("", token, body, Map.class);

    }


}




